﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using ATA.Entities;
using ATA.Data;

#endregion

namespace ATA.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="KhachHangDuthuongProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class KhachHangDuthuongProviderBaseCore : EntityProviderBase<ATA.Entities.KhachHangDuthuong, ATA.Entities.KhachHangDuthuongKey>
	{		
		#region Get from Many To Many Relationship Functions
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, ATA.Entities.KhachHangDuthuongKey key)
		{
			return Delete(transactionManager, key.Id);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="id">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(System.Guid id)
		{
			return Delete(null, id);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="id">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, System.Guid id);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_KhachHangDuthuong_ChuongTrinh key.
		///		FK_KhachHangDuthuong_ChuongTrinh Description: 
		/// </summary>
		/// <param name="chuongTrinhId"></param>
		/// <returns>Returns a typed collection of ATA.Entities.KhachHangDuthuong objects.</returns>
		public ATA.Entities.TList<KhachHangDuthuong> GetByChuongTrinhId(System.Int32? chuongTrinhId)
		{
			int count = -1;
			return GetByChuongTrinhId(chuongTrinhId, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_KhachHangDuthuong_ChuongTrinh key.
		///		FK_KhachHangDuthuong_ChuongTrinh Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="chuongTrinhId"></param>
		/// <returns>Returns a typed collection of ATA.Entities.KhachHangDuthuong objects.</returns>
		/// <remarks></remarks>
		public ATA.Entities.TList<KhachHangDuthuong> GetByChuongTrinhId(TransactionManager transactionManager, System.Int32? chuongTrinhId)
		{
			int count = -1;
			return GetByChuongTrinhId(transactionManager, chuongTrinhId, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_KhachHangDuthuong_ChuongTrinh key.
		///		FK_KhachHangDuthuong_ChuongTrinh Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="chuongTrinhId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.KhachHangDuthuong objects.</returns>
		public ATA.Entities.TList<KhachHangDuthuong> GetByChuongTrinhId(TransactionManager transactionManager, System.Int32? chuongTrinhId, int start, int pageLength)
		{
			int count = -1;
			return GetByChuongTrinhId(transactionManager, chuongTrinhId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_KhachHangDuthuong_ChuongTrinh key.
		///		fkKhachHangDuthuongChuongTrinh Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="chuongTrinhId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.KhachHangDuthuong objects.</returns>
		public ATA.Entities.TList<KhachHangDuthuong> GetByChuongTrinhId(System.Int32? chuongTrinhId, int start, int pageLength)
		{
			int count =  -1;
			return GetByChuongTrinhId(null, chuongTrinhId, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_KhachHangDuthuong_ChuongTrinh key.
		///		fkKhachHangDuthuongChuongTrinh Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="chuongTrinhId"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.KhachHangDuthuong objects.</returns>
		public ATA.Entities.TList<KhachHangDuthuong> GetByChuongTrinhId(System.Int32? chuongTrinhId, int start, int pageLength,out int count)
		{
			return GetByChuongTrinhId(null, chuongTrinhId, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_KhachHangDuthuong_ChuongTrinh key.
		///		FK_KhachHangDuthuong_ChuongTrinh Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="chuongTrinhId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of ATA.Entities.KhachHangDuthuong objects.</returns>
		public abstract ATA.Entities.TList<KhachHangDuthuong> GetByChuongTrinhId(TransactionManager transactionManager, System.Int32? chuongTrinhId, int start, int pageLength, out int count);
		
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_KhachHangDuthuong_KhachHangDuthuong key.
		///		FK_KhachHangDuthuong_KhachHangDuthuong Description: 
		/// </summary>
		/// <param name="brandId"></param>
		/// <returns>Returns a typed collection of ATA.Entities.KhachHangDuthuong objects.</returns>
		public ATA.Entities.TList<KhachHangDuthuong> GetByBrandId(System.Int32 brandId)
		{
			int count = -1;
			return GetByBrandId(brandId, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_KhachHangDuthuong_KhachHangDuthuong key.
		///		FK_KhachHangDuthuong_KhachHangDuthuong Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="brandId"></param>
		/// <returns>Returns a typed collection of ATA.Entities.KhachHangDuthuong objects.</returns>
		/// <remarks></remarks>
		public ATA.Entities.TList<KhachHangDuthuong> GetByBrandId(TransactionManager transactionManager, System.Int32 brandId)
		{
			int count = -1;
			return GetByBrandId(transactionManager, brandId, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_KhachHangDuthuong_KhachHangDuthuong key.
		///		FK_KhachHangDuthuong_KhachHangDuthuong Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="brandId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.KhachHangDuthuong objects.</returns>
		public ATA.Entities.TList<KhachHangDuthuong> GetByBrandId(TransactionManager transactionManager, System.Int32 brandId, int start, int pageLength)
		{
			int count = -1;
			return GetByBrandId(transactionManager, brandId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_KhachHangDuthuong_KhachHangDuthuong key.
		///		fkKhachHangDuthuongKhachHangDuthuong Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="brandId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.KhachHangDuthuong objects.</returns>
		public ATA.Entities.TList<KhachHangDuthuong> GetByBrandId(System.Int32 brandId, int start, int pageLength)
		{
			int count =  -1;
			return GetByBrandId(null, brandId, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_KhachHangDuthuong_KhachHangDuthuong key.
		///		fkKhachHangDuthuongKhachHangDuthuong Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="brandId"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.KhachHangDuthuong objects.</returns>
		public ATA.Entities.TList<KhachHangDuthuong> GetByBrandId(System.Int32 brandId, int start, int pageLength,out int count)
		{
			return GetByBrandId(null, brandId, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_KhachHangDuthuong_KhachHangDuthuong key.
		///		FK_KhachHangDuthuong_KhachHangDuthuong Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="brandId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of ATA.Entities.KhachHangDuthuong objects.</returns>
		public abstract ATA.Entities.TList<KhachHangDuthuong> GetByBrandId(TransactionManager transactionManager, System.Int32 brandId, int start, int pageLength, out int count);
		
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_KhachHangDuthuong_PhanThuong key.
		///		FK_KhachHangDuthuong_PhanThuong Description: 
		/// </summary>
		/// <param name="phanThuongId"></param>
		/// <returns>Returns a typed collection of ATA.Entities.KhachHangDuthuong objects.</returns>
		public ATA.Entities.TList<KhachHangDuthuong> GetByPhanThuongId(System.Int32? phanThuongId)
		{
			int count = -1;
			return GetByPhanThuongId(phanThuongId, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_KhachHangDuthuong_PhanThuong key.
		///		FK_KhachHangDuthuong_PhanThuong Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="phanThuongId"></param>
		/// <returns>Returns a typed collection of ATA.Entities.KhachHangDuthuong objects.</returns>
		/// <remarks></remarks>
		public ATA.Entities.TList<KhachHangDuthuong> GetByPhanThuongId(TransactionManager transactionManager, System.Int32? phanThuongId)
		{
			int count = -1;
			return GetByPhanThuongId(transactionManager, phanThuongId, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_KhachHangDuthuong_PhanThuong key.
		///		FK_KhachHangDuthuong_PhanThuong Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="phanThuongId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.KhachHangDuthuong objects.</returns>
		public ATA.Entities.TList<KhachHangDuthuong> GetByPhanThuongId(TransactionManager transactionManager, System.Int32? phanThuongId, int start, int pageLength)
		{
			int count = -1;
			return GetByPhanThuongId(transactionManager, phanThuongId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_KhachHangDuthuong_PhanThuong key.
		///		fkKhachHangDuthuongPhanThuong Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="phanThuongId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.KhachHangDuthuong objects.</returns>
		public ATA.Entities.TList<KhachHangDuthuong> GetByPhanThuongId(System.Int32? phanThuongId, int start, int pageLength)
		{
			int count =  -1;
			return GetByPhanThuongId(null, phanThuongId, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_KhachHangDuthuong_PhanThuong key.
		///		fkKhachHangDuthuongPhanThuong Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="phanThuongId"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.KhachHangDuthuong objects.</returns>
		public ATA.Entities.TList<KhachHangDuthuong> GetByPhanThuongId(System.Int32? phanThuongId, int start, int pageLength,out int count)
		{
			return GetByPhanThuongId(null, phanThuongId, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_KhachHangDuthuong_PhanThuong key.
		///		FK_KhachHangDuthuong_PhanThuong Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="phanThuongId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of ATA.Entities.KhachHangDuthuong objects.</returns>
		public abstract ATA.Entities.TList<KhachHangDuthuong> GetByPhanThuongId(TransactionManager transactionManager, System.Int32? phanThuongId, int start, int pageLength, out int count);
		
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override ATA.Entities.KhachHangDuthuong Get(TransactionManager transactionManager, ATA.Entities.KhachHangDuthuongKey key, int start, int pageLength)
		{
			return GetById(transactionManager, key.Id, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key PK_KhachHangDuthuong index.
		/// </summary>
		/// <param name="id"></param>
		/// <returns>Returns an instance of the <see cref="ATA.Entities.KhachHangDuthuong"/> class.</returns>
		public ATA.Entities.KhachHangDuthuong GetById(System.Guid id)
		{
			int count = -1;
			return GetById(null,id, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_KhachHangDuthuong index.
		/// </summary>
		/// <param name="id"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="ATA.Entities.KhachHangDuthuong"/> class.</returns>
		public ATA.Entities.KhachHangDuthuong GetById(System.Guid id, int start, int pageLength)
		{
			int count = -1;
			return GetById(null, id, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_KhachHangDuthuong index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="id"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="ATA.Entities.KhachHangDuthuong"/> class.</returns>
		public ATA.Entities.KhachHangDuthuong GetById(TransactionManager transactionManager, System.Guid id)
		{
			int count = -1;
			return GetById(transactionManager, id, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_KhachHangDuthuong index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="id"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="ATA.Entities.KhachHangDuthuong"/> class.</returns>
		public ATA.Entities.KhachHangDuthuong GetById(TransactionManager transactionManager, System.Guid id, int start, int pageLength)
		{
			int count = -1;
			return GetById(transactionManager, id, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_KhachHangDuthuong index.
		/// </summary>
		/// <param name="id"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="ATA.Entities.KhachHangDuthuong"/> class.</returns>
		public ATA.Entities.KhachHangDuthuong GetById(System.Guid id, int start, int pageLength, out int count)
		{
			return GetById(null, id, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_KhachHangDuthuong index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="id"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="ATA.Entities.KhachHangDuthuong"/> class.</returns>
		public abstract ATA.Entities.KhachHangDuthuong GetById(TransactionManager transactionManager, System.Guid id, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#region _KhachHangDuthuong_GetListByCondition 
		
		/// <summary>
		///	This method wrap the '_KhachHangDuthuong_GetListByCondition' stored procedure. 
		/// </summary>
		/// <param name="whereClause"> A <c>System.String</c> instance.</param>
		/// <remark>This method is generate from a stored procedure.</remark>
		public void GetListByCondition(System.String whereClause)
		{
			 GetListByCondition(null, 0, int.MaxValue , whereClause);
		}
		
		/// <summary>
		///	This method wrap the '_KhachHangDuthuong_GetListByCondition' stored procedure. 
		/// </summary>
		/// <param name="whereClause"> A <c>System.String</c> instance.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remark>This method is generate from a stored procedure.</remark>
		public void GetListByCondition(int start, int pageLength, System.String whereClause)
		{
			 GetListByCondition(null, start, pageLength , whereClause);
		}
				
		/// <summary>
		///	This method wrap the '_KhachHangDuthuong_GetListByCondition' stored procedure. 
		/// </summary>
		/// <param name="whereClause"> A <c>System.String</c> instance.</param>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <remark>This method is generate from a stored procedure.</remark>
		public void GetListByCondition(TransactionManager transactionManager, System.String whereClause)
		{
			 GetListByCondition(transactionManager, 0, int.MaxValue , whereClause);
		}
		
		/// <summary>
		///	This method wrap the '_KhachHangDuthuong_GetListByCondition' stored procedure. 
		/// </summary>
		/// <param name="whereClause"> A <c>System.String</c> instance.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <remark>This method is generate from a stored procedure.</remark>
		public abstract void GetListByCondition(TransactionManager transactionManager, int start, int pageLength , System.String whereClause);
		
		#endregion
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a ATA.Entities.TList&lt;KhachHangDuthuong&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="ATA.Entities.TList&lt;KhachHangDuthuong&gt;"/></returns>
		public static ATA.Entities.TList<KhachHangDuthuong> Fill(IDataReader reader, ATA.Entities.TList<KhachHangDuthuong> rows, int start, int pageLength)
		{
		// advance to the starting row
		for (int i = 0; i < start; i++)
		{
			if (!reader.Read())
			return rows; // not enough rows, just return
		}
		for (int i = 0; i < pageLength; i++)
		{
			if (!reader.Read())
			break; // we are done
			string key = null;
			
			ATA.Entities.KhachHangDuthuong c = null;
			if (DataRepository.Provider.UseEntityFactory)
			{
			key = new System.Text.StringBuilder("KhachHangDuthuong")
			.Append("|").Append((reader.IsDBNull(((int)ATA.Entities.KhachHangDuthuongColumn.Id - 1))?Guid.Empty:(System.Guid)reader[((int)ATA.Entities.KhachHangDuthuongColumn.Id - 1)]).ToString()).ToString();
			c = EntityManager.LocateOrCreate<KhachHangDuthuong>(
			key.ToString(), // EntityTrackingKey
			"KhachHangDuthuong",  //Creational Type
			DataRepository.Provider.EntityCreationalFactoryType,  //Factory used to create entity
			DataRepository.Provider.EnableEntityTracking); // Track this entity?
			}
			else
			{
			c = new ATA.Entities.KhachHangDuthuong();
			}
			
			if (!DataRepository.Provider.EnableEntityTracking ||
			c.EntityState == EntityState.Added ||
			(DataRepository.Provider.EnableEntityTracking &&
			((DataRepository.Provider.CurrentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
			(DataRepository.Provider.CurrentLoadPolicy == LoadPolicy.DiscardChanges && (c.EntityState == EntityState.Unchanged ||
								c.EntityState == EntityState.Changed)))))
						{
			c.SuppressEntityEvents = true;
			c.Id = (System.Guid)reader["ID"];
			c.OriginalId = c.Id;
			c.HoiVienId = (System.Guid)reader["HoiVienID"];
			c.Duration = (System.Int32)reader["Duration"];
			c.StartDate = (System.DateTime)reader["StartDate"];
			c.EndDate = (System.DateTime)reader["EndDate"];
			c.BrandId = (System.Int32)reader["BrandID"];
			c.IsActive = (System.Boolean)reader["IsActive"];
			c.ChuongTrinhId = reader.IsDBNull(reader.GetOrdinal("ChuongTrinhID")) ? null : (System.Int32?)reader["ChuongTrinhID"];
			c.LoaiSanPham = reader.IsDBNull(reader.GetOrdinal("LoaiSanPham")) ? null : (System.String)reader["LoaiSanPham"];
			c.MucChietKhauTd = reader.IsDBNull(reader.GetOrdinal("MucChietKhauTD")) ? null : (System.Decimal?)reader["MucChietKhauTD"];
			c.MucChietKhauCc = reader.IsDBNull(reader.GetOrdinal("MucChietKhauCC")) ? null : (System.Decimal?)reader["MucChietKhauCC"];
			c.UpdatedDate = reader.IsDBNull(reader.GetOrdinal("UpdatedDate")) ? null : (System.DateTime?)reader["UpdatedDate"];
			c.UpdatedBy = reader.IsDBNull(reader.GetOrdinal("UpdatedBy")) ? null : (System.Int32?)reader["UpdatedBy"];
			c.PhanThuongId = reader.IsDBNull(reader.GetOrdinal("PhanThuongID")) ? null : (System.Int32?)reader["PhanThuongID"];
			c.SoBienBan = reader.IsDBNull(reader.GetOrdinal("SoBienBan")) ? null : (System.String)reader["SoBienBan"];
			c.NgayKyBienBan = reader.IsDBNull(reader.GetOrdinal("NgayKyBienBan")) ? null : (System.DateTime?)reader["NgayKyBienBan"];
			c.EntityTrackingKey = key;
			c.AcceptChanges();
			c.SuppressEntityEvents = false;
			}
			rows.Add(c);
		}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="ATA.Entities.KhachHangDuthuong"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="ATA.Entities.KhachHangDuthuong"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, ATA.Entities.KhachHangDuthuong entity)
		{
			if (!reader.Read()) return;
			
			entity.Id = (System.Guid)reader["ID"];
			entity.OriginalId = (System.Guid)reader["ID"];
			entity.HoiVienId = (System.Guid)reader["HoiVienID"];
			entity.Duration = (System.Int32)reader["Duration"];
			entity.StartDate = (System.DateTime)reader["StartDate"];
			entity.EndDate = (System.DateTime)reader["EndDate"];
			entity.BrandId = (System.Int32)reader["BrandID"];
			entity.IsActive = (System.Boolean)reader["IsActive"];
			entity.ChuongTrinhId = reader.IsDBNull(reader.GetOrdinal("ChuongTrinhID")) ? null : (System.Int32?)reader["ChuongTrinhID"];
			entity.LoaiSanPham = reader.IsDBNull(reader.GetOrdinal("LoaiSanPham")) ? null : (System.String)reader["LoaiSanPham"];
			entity.MucChietKhauTd = reader.IsDBNull(reader.GetOrdinal("MucChietKhauTD")) ? null : (System.Decimal?)reader["MucChietKhauTD"];
			entity.MucChietKhauCc = reader.IsDBNull(reader.GetOrdinal("MucChietKhauCC")) ? null : (System.Decimal?)reader["MucChietKhauCC"];
			entity.UpdatedDate = reader.IsDBNull(reader.GetOrdinal("UpdatedDate")) ? null : (System.DateTime?)reader["UpdatedDate"];
			entity.UpdatedBy = reader.IsDBNull(reader.GetOrdinal("UpdatedBy")) ? null : (System.Int32?)reader["UpdatedBy"];
			entity.PhanThuongId = reader.IsDBNull(reader.GetOrdinal("PhanThuongID")) ? null : (System.Int32?)reader["PhanThuongID"];
			entity.SoBienBan = reader.IsDBNull(reader.GetOrdinal("SoBienBan")) ? null : (System.String)reader["SoBienBan"];
			entity.NgayKyBienBan = reader.IsDBNull(reader.GetOrdinal("NgayKyBienBan")) ? null : (System.DateTime?)reader["NgayKyBienBan"];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="ATA.Entities.KhachHangDuthuong"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="ATA.Entities.KhachHangDuthuong"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, ATA.Entities.KhachHangDuthuong entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.Id = (System.Guid)dataRow["ID"];
			entity.OriginalId = (System.Guid)dataRow["ID"];
			entity.HoiVienId = (System.Guid)dataRow["HoiVienID"];
			entity.Duration = (System.Int32)dataRow["Duration"];
			entity.StartDate = (System.DateTime)dataRow["StartDate"];
			entity.EndDate = (System.DateTime)dataRow["EndDate"];
			entity.BrandId = (System.Int32)dataRow["BrandID"];
			entity.IsActive = (System.Boolean)dataRow["IsActive"];
			entity.ChuongTrinhId = Convert.IsDBNull(dataRow["ChuongTrinhID"]) ? null : (System.Int32?)dataRow["ChuongTrinhID"];
			entity.LoaiSanPham = Convert.IsDBNull(dataRow["LoaiSanPham"]) ? null : (System.String)dataRow["LoaiSanPham"];
			entity.MucChietKhauTd = Convert.IsDBNull(dataRow["MucChietKhauTD"]) ? null : (System.Decimal?)dataRow["MucChietKhauTD"];
			entity.MucChietKhauCc = Convert.IsDBNull(dataRow["MucChietKhauCC"]) ? null : (System.Decimal?)dataRow["MucChietKhauCC"];
			entity.UpdatedDate = Convert.IsDBNull(dataRow["UpdatedDate"]) ? null : (System.DateTime?)dataRow["UpdatedDate"];
			entity.UpdatedBy = Convert.IsDBNull(dataRow["UpdatedBy"]) ? null : (System.Int32?)dataRow["UpdatedBy"];
			entity.PhanThuongId = Convert.IsDBNull(dataRow["PhanThuongID"]) ? null : (System.Int32?)dataRow["PhanThuongID"];
			entity.SoBienBan = Convert.IsDBNull(dataRow["SoBienBan"]) ? null : (System.String)dataRow["SoBienBan"];
			entity.NgayKyBienBan = Convert.IsDBNull(dataRow["NgayKyBienBan"]) ? null : (System.DateTime?)dataRow["NgayKyBienBan"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="ATA.Entities.KhachHangDuthuong"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">ATA.Entities.KhachHangDuthuong Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		internal override void DeepLoad(TransactionManager transactionManager, ATA.Entities.KhachHangDuthuong entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;

			#region ChuongTrinhIdSource	
			if (CanDeepLoad(entity, "ChuongTrinh|ChuongTrinhIdSource", deepLoadType, innerList) 
				&& entity.ChuongTrinhIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = (entity.ChuongTrinhId ?? (int)0);
				ChuongTrinh tmpEntity = EntityManager.LocateEntity<ChuongTrinh>(EntityLocator.ConstructKeyFromPkItems(typeof(ChuongTrinh), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.ChuongTrinhIdSource = tmpEntity;
				else
					entity.ChuongTrinhIdSource = DataRepository.ChuongTrinhProvider.GetByChuongTrinhId(transactionManager, (entity.ChuongTrinhId ?? (int)0));		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'ChuongTrinhIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.ChuongTrinhIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.ChuongTrinhProvider.DeepLoad(transactionManager, entity.ChuongTrinhIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion ChuongTrinhIdSource

			#region BrandIdSource	
			if (CanDeepLoad(entity, "ThuongHieu|BrandIdSource", deepLoadType, innerList) 
				&& entity.BrandIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.BrandId;
				ThuongHieu tmpEntity = EntityManager.LocateEntity<ThuongHieu>(EntityLocator.ConstructKeyFromPkItems(typeof(ThuongHieu), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.BrandIdSource = tmpEntity;
				else
					entity.BrandIdSource = DataRepository.ThuongHieuProvider.GetByBrandId(transactionManager, entity.BrandId);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'BrandIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.BrandIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.ThuongHieuProvider.DeepLoad(transactionManager, entity.BrandIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion BrandIdSource

			#region PhanThuongIdSource	
			if (CanDeepLoad(entity, "PhanThuong|PhanThuongIdSource", deepLoadType, innerList) 
				&& entity.PhanThuongIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = (entity.PhanThuongId ?? (int)0);
				PhanThuong tmpEntity = EntityManager.LocateEntity<PhanThuong>(EntityLocator.ConstructKeyFromPkItems(typeof(PhanThuong), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.PhanThuongIdSource = tmpEntity;
				else
					entity.PhanThuongIdSource = DataRepository.PhanThuongProvider.GetById(transactionManager, (entity.PhanThuongId ?? (int)0));		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'PhanThuongIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.PhanThuongIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.PhanThuongProvider.DeepLoad(transactionManager, entity.PhanThuongIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion PhanThuongIdSource
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the ATA.Entities.KhachHangDuthuong object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">ATA.Entities.KhachHangDuthuong instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">ATA.Entities.KhachHangDuthuong Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		internal override bool DeepSave(TransactionManager transactionManager, ATA.Entities.KhachHangDuthuong entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			
			#region ChuongTrinhIdSource
			if (CanDeepSave(entity, "ChuongTrinh|ChuongTrinhIdSource", deepSaveType, innerList) 
				&& entity.ChuongTrinhIdSource != null)
			{
				DataRepository.ChuongTrinhProvider.Save(transactionManager, entity.ChuongTrinhIdSource);
				entity.ChuongTrinhId = entity.ChuongTrinhIdSource.ChuongTrinhId;
			}
			#endregion 
			
			#region BrandIdSource
			if (CanDeepSave(entity, "ThuongHieu|BrandIdSource", deepSaveType, innerList) 
				&& entity.BrandIdSource != null)
			{
				DataRepository.ThuongHieuProvider.Save(transactionManager, entity.BrandIdSource);
				entity.BrandId = entity.BrandIdSource.BrandId;
			}
			#endregion 
			
			#region PhanThuongIdSource
			if (CanDeepSave(entity, "PhanThuong|PhanThuongIdSource", deepSaveType, innerList) 
				&& entity.PhanThuongIdSource != null)
			{
				DataRepository.PhanThuongProvider.Save(transactionManager, entity.PhanThuongIdSource);
				entity.PhanThuongId = entity.PhanThuongIdSource.Id;
			}
			#endregion 
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<Delegate, object> deepHandles = new Dictionary<Delegate, object>();
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region KhachHangDuthuongChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>ATA.Entities.KhachHangDuthuong</c>
	///</summary>
	public enum KhachHangDuthuongChildEntityTypes
	{
		
		///<summary>
		/// Composite Property for <c>ChuongTrinh</c> at ChuongTrinhIdSource
		///</summary>
		[ChildEntityType(typeof(ChuongTrinh))]
		ChuongTrinh,
			
		///<summary>
		/// Composite Property for <c>ThuongHieu</c> at BrandIdSource
		///</summary>
		[ChildEntityType(typeof(ThuongHieu))]
		ThuongHieu,
			
		///<summary>
		/// Composite Property for <c>PhanThuong</c> at PhanThuongIdSource
		///</summary>
		[ChildEntityType(typeof(PhanThuong))]
		PhanThuong,
		}
	
	#endregion KhachHangDuthuongChildEntityTypes
	
	#region KhachHangDuthuongFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;KhachHangDuthuongColumn&gt;"/> class
	/// that is used exclusively with a <see cref="KhachHangDuthuong"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class KhachHangDuthuongFilterBuilder : SqlFilterBuilder<KhachHangDuthuongColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the KhachHangDuthuongFilterBuilder class.
		/// </summary>
		public KhachHangDuthuongFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the KhachHangDuthuongFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public KhachHangDuthuongFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the KhachHangDuthuongFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public KhachHangDuthuongFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion KhachHangDuthuongFilterBuilder
	
	#region KhachHangDuthuongParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;KhachHangDuthuongColumn&gt;"/> class
	/// that is used exclusively with a <see cref="KhachHangDuthuong"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class KhachHangDuthuongParameterBuilder : ParameterizedSqlFilterBuilder<KhachHangDuthuongColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the KhachHangDuthuongParameterBuilder class.
		/// </summary>
		public KhachHangDuthuongParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the KhachHangDuthuongParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public KhachHangDuthuongParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the KhachHangDuthuongParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public KhachHangDuthuongParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion KhachHangDuthuongParameterBuilder
} // end namespace
